import{reqCode,reqRegister,reqLogin} from '@/api'
const state ={
     code:'',
     token:''
}
const mutations ={
    GETCODE(state,code){
        state.code = code;
    },
    SETTOKEN(state,token){
         state.token = token;
    }
}
const actions = {
    //获取验证码
 async getCode({commit},phone){
  let result =  await reqCode(phone);
  //前端工程师：没有能力把验证码发到用户手机上功能
  //务必,一定是后台人员实现的
        //测试接收验证码
        // console.log(result)
        if(result.code == 200){
            commit("GETCODE",result.data);
            return 'ok';
        }else{
            return Promise.reject(new Error('faile'));
        }
   },
   //用户注册
   async userRegister({commit},{phone,code,password}){
       let result = await reqRegister(phone,code,password);
       //测试打印注册注册后服务器返回的结果
        // console.log(result)
       if(result.code == 200){
            return 'ok';
       }else{
            //捕获到错误信息
            return Promise.reject(result.message);
       }

   },
   //登录业务
   async userLogin({commit},{phone,password}){
       //当前的这个action,向服务器发起登录请求
       let result = await reqLogin(phone,password);
       //测试打印用户登录成功后服务器返回
       console.log(result);
       if(result.code==200){
             //如果仓库，已经存储token,用户一定是登录了
             commit("SETTOKEN",result.data.token);
             return 'ok';
       }else{
            //登录失败 
            return Promise.reject(result.message);
       }
   }
};
const getters = {

}
export default{
    state,
    mutations,
    actions,
    getters
}
